package com.fenghuajueli.libbasecoreui.upload.oppo

import android.app.Application
import android.os.Build
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.fenghuajueli.libbasecoreui.upload.BaseUpload
import com.fenghuajueli.libbasecoreui.upload.Channel
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

open class OppoUploadUtils : BaseUpload() {

    companion object {
        @JvmStatic
        val instance: OppoUploadUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            OppoUploadUtils()
        }
    }

    /**
     * 打开应用时检查
     * 首次安装
     * 次日留存
     * @param lifecycleOwner
     * @param application
     */
    @Deprecated("统一在MyApplication调用", replaceWith = ReplaceWith("openAction(application: Application)"))
    fun openAction(lifecycleOwner: LifecycleOwner, application: Application) {
        lifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
            action(Channel.OPPO) {
                if (checkIsFirstInstall(application)) {
                    MmkvUtils.save(FIRST_LAUNCH, false)
                    LogUtils.d("firstInstallAction: 首次安装启动")
                    uploadActiveData(application, OppoDataType.ACTIVATE_TYPE, null)
                } else if (checkSeconddayRetention()) {
                    LogUtils.d("firstInstallAction: 次留")
                    uploadActiveData(application, OppoDataType.TOW_DAY, null)
                }
            }
        }
    }

    fun openAction(application: Application) {
        appScope.launch {
            action(Channel.OPPO) {
                if (checkIsFirstInstall(application)) {
                    MmkvUtils.save(FIRST_LAUNCH, false)
                    LogUtils.d("firstInstallAction: 首次安装启动")
                    uploadActiveData(application, OppoDataType.ACTIVATE_TYPE, null)
                } else if (checkSeconddayRetention()) {
                    LogUtils.d("firstInstallAction: 次留")
                    uploadActiveData(application, OppoDataType.TOW_DAY, null)
                }
            }
        }
    }

    /**
     * 注册
     * 注册完3分钟内算新注册用户
     * @param needCheck 一键登录、微信登录需要传true
     * @param application
     */
    @Deprecated("统一在MyApplication调用", replaceWith = ReplaceWith("registerAction(application: Application)"))
    fun registerAction(lifecycleOwner: LifecycleOwner, application: Application, needCheck: Boolean = false) {
        lifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
            action(Channel.OPPO) {
                if (needCheck) {
                    if (checkIsRegister()) {
                        uploadActiveData(application, OppoDataType.REGISTERED, null)
                    }
                } else {
                    uploadActiveData(application, OppoDataType.REGISTERED, null)
                }
            }
        }
    }

    fun registerAction(application: Application) {
        appScope.launch {
            action(Channel.OPPO) {
                uploadActiveData(application, OppoDataType.REGISTERED, null)
            }
        }
    }


    /**
     * 付费
     * 7天内付费才上传
     * @param lifecycleOwner
     * @param application
     * @param payAmount
     */
    @Deprecated("统一在MyApplication调用", replaceWith = ReplaceWith("payAction(application: Application, payAmount: Double)"))
    fun payAction(lifecycleOwner: LifecycleOwner, application: Application, payAmount: Double) {
        lifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
            action(Channel.OPPO) {
                if (checkPayWithinSevendays()) {
                    uploadActiveData(application, OppoDataType.APP_PAY, (payAmount * 100).toLong())
                }
            }
        }
    }

    fun payAction(application: Application, payAmount: Double) {
        appScope.launch {
            action(Channel.OPPO) {
                if (checkPayWithinSevendays()) {
                    uploadActiveData(application, OppoDataType.APP_PAY, (payAmount * 100).toLong())
                }
            }
        }
    }

    /**
     * oppo转化数据同步接口
     *
     * @param dataType
     * @param payAmount
     */
    private suspend fun uploadActiveData(application: Application, @OppoDataType dataType: Int, payAmount: Long?) {
        application.uploadActiveData(Channel.OPPO) {
            val contentsMap = hashMapOf<String, Any>()
            contentsMap.apply {
                //OAID
                contentsMap["ouId"] = oaid!!
                //事件发生的系统时间戳
                contentsMap["timestamp"] = System.currentTimeMillis()
                //包名
                contentsMap["pkg"] = application.packageName
                //付费金额
                payAmount?.let { contentsMap["payAmount"] = it }
                //事件类型
                contentsMap["dataType"] = dataType
                //商标
                contentsMap["brand"] = Build.BRAND
                //厂商
                contentsMap["manufacturer"] = Build.MANUFACTURER
                //机型
                contentsMap["model"] = Build.MODEL
            }

            GsonUtils.toJson(contentsMap)
        }
    }

}